﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for CookieManager
/// </summary>
public static class CookieManager
{
    #region Constansts
    private const string COOKIE_NAME = "Obligatorio";
    #endregion

    public static void CreateCookie()
    {
        HttpCookie cookie = new HttpCookie(COOKIE_NAME);
        cookie.Expires = DateTime.Now.AddYears(1);

        HttpContext.Current.Response.Cookies.Add(cookie);
    }

    public static HttpCookie GetCookie()
    {
        return (HttpContext.Current.Request.Cookies.Get(COOKIE_NAME));
    }

    public static void DeleteCookie()
    {
        if (HttpContext.Current.Request.Cookies[COOKIE_NAME] != null)
        {
            HttpCookie myCookie = new HttpCookie(COOKIE_NAME);
            myCookie.Expires = DateTime.Now.AddDays(-1d);
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }
    }

    public static void SetValue(string key, string value)
    {
        HttpCookie cookie = GetCookie();

        if (cookie == null)
            cookie = new HttpCookie(COOKIE_NAME);

        cookie.Values.Set(key, value);

        cookie.Expires = DateTime.Now.AddYears(1);

        HttpContext.Current.Response.Cookies.Add(cookie);
    }

    public static string GetValue(string key)
    {
        HttpCookie cookie = GetCookie();

        if (cookie != null)
            return (cookie.Values.Get(key));
        else return string.Empty;
    }

    public static void RenewCookie()
    {
        HttpCookie cookie = GetCookie();

        if (cookie != null)
            cookie.Expires = DateTime.Now.AddYears(1);
        else CreateCookie();
    }

}